package org.example.springboot.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.springboot.common.Result;
import org.example.springboot.service.RatingService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 评分控制器
 */
@Tag(name="评分管理接口")
@RestController
@RequestMapping("/rating")
public class RatingController {
    @Resource
    private RatingService ratingService;
    
    @Operation(summary = "给图书评分")
    @PostMapping("/add")
    public Result<?> rateBook(@RequestBody Map<String, Object> params) {
        Long bookId = Long.valueOf(params.get("bookId").toString());
        Integer score = Integer.valueOf(params.get("score").toString());
        
        if (bookId == null) {
            return Result.error("图书ID不能为空");
        }
        
        if (score == null || score < 1 || score > 5) {
            return Result.error("评分必须在1-5之间");
        }
        
        Long ratingId = ratingService.rateBook(bookId, score);
        return Result.success("评分成功", ratingId);
    }
    
    @Operation(summary = "获取用户对图书的评分")
    @GetMapping("/user")
    public Result<?> getUserRating(@RequestParam Long bookId) {
        Integer score = ratingService.getUserRating(bookId);
        return Result.success(score);
    }
} 